package kit.views;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import kit.persistency.KitDB;

import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;

public class AnswerPanel extends JPanel {
	private JCheckBox chckbxNewCheckBox;
	private JList list;
	private String question;

	/**
	 * Create the panel.
	 */
	public AnswerPanel(String question) {
		this.question = question;
		
		setLayout(new BorderLayout(0, 0));

		JPanel panel_1 = new JPanel();
		add(panel_1, BorderLayout.CENTER);
		panel_1.setLayout(new BorderLayout(0, 0));

		JPanel panel_2 = new JPanel();
		panel_1.add(panel_2, BorderLayout.CENTER);
		panel_2.setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC, FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"), }, new RowSpec[] {
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_ROWSPEC, FormFactory.DEFAULT_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC, }));

		JLabel lblTesto = new JLabel("Risposte:");
		panel_2.add(lblTesto, "2, 2, default, top");

		list = new JList();
		list.addListSelectionListener(new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent arg0) {
				KitDB db = new KitDB();
				String answer = (String) AnswerPanel.this.getAnswersList()
						.getSelectedValue();
				answer = answer.split(" ",2)[0];
				AnswerPanel.this.getExposedAnswerCheckbox().setSelected(
						db.isAnswerExposed(AnswerPanel.this.question, answer));
				AnswerPanel.this.repaint();
			}
		});
		list.setBorder(new LineBorder(new Color(0, 0, 0)));
		panel_2.add(list, "4, 2, fill, fill");
		list.setCellRenderer(new ExposedCellRender(question));
		
		JLabel lblEStataEsposta = new JLabel("E' stata esposta?");
		panel_2.add(lblEStataEsposta, "2, 4");

		chckbxNewCheckBox = new JCheckBox("");
		chckbxNewCheckBox.setEnabled(false);
		panel_2.add(chckbxNewCheckBox, "4, 4");

	}

	public JCheckBox getExposedAnswerCheckbox() {
		return chckbxNewCheckBox;
	}

	public JList getAnswersList() {
		return list;
	}
}
